/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroPaciente.java
 *
 * Created on 02/05/2011, 16:42:28
 */
package br.com.clinicsolution.apresentacao;

import br.com.clinicsolution.negocio.IPacienteBO;
import br.com.clinicsolution.negocio.impl.PacienteBO;
import br.com.clinicsolution.persistencia.model.Cidade;
import br.com.clinicsolution.persistencia.model.Convenio;
import br.com.clinicsolution.persistencia.model.CorPessoa;
import br.com.clinicsolution.persistencia.model.EstadoCivil;
import br.com.clinicsolution.persistencia.model.Paciente;
import br.com.clinicsolution.persistencia.model.Plano;
import br.com.clinicsolution.persistencia.model.Profissao;
import br.com.clinicsolution.persistencia.model.Uf;
import java.sql.Date;
import java.text.ParseException;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Patricia Rocheulle
 */
public class CadastroPaciente extends JInternalFrame {

    private Paciente paciente;
    private static MaskFormatter mascCPF;
    private static MaskFormatter mascTel;

    static {

        try {
            mascCPF = new MaskFormatter("###########");
            mascTel = new MaskFormatter("####-####");

        } catch (ParseException ex) {
            ex.printStackTrace();
        }

    }

    /** Creates new form CadastroPaciente */
    public CadastroPaciente() {

        initComponents();

    }

    private MaskFormatter getMask(String formato) {
        MaskFormatter fmtData;
        try {
            fmtData = new MaskFormatter(formato);
            return fmtData;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gpSexo = new javax.swing.ButtonGroup();
        pDadosPessoais = new javax.swing.JPanel();
        txtCpf = new javax.swing.JFormattedTextField(getMask("#########-##"));
        txtRg = new javax.swing.JFormattedTextField(getMask("###########"));
        txtOrgaoEmissorRg = new javax.swing.JFormattedTextField(getMask("***-**"));
        txtDataEmissao = new javax.swing.JFormattedTextField(getMask("##/##/####"));
        txtDataNascimento = new javax.swing.JFormattedTextField(getMask("##/##/####"));
        txtNome = new javax.swing.JTextField();
        txtApelido = new javax.swing.JTextField();
        txtIdade = new javax.swing.JTextField();
        rbSexoFeminino = new javax.swing.JRadioButton();
        rbSexoMasculino = new javax.swing.JRadioButton();
        cbCor = new javax.swing.JComboBox();
        cbProfissao = new javax.swing.JComboBox();
        cbEstadoCivil = new javax.swing.JComboBox();
        txtNomeMae = new javax.swing.JTextField();
        txtNomePai = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        pEndereco = new javax.swing.JPanel();
        txtEndResidencial = new javax.swing.JTextField();
        txtNumero = new javax.swing.JTextField();
        txtComplemento = new javax.swing.JTextField();
        txtBairro = new javax.swing.JTextField();
        txtCep = new javax.swing.JFormattedTextField(getMask("#####-###"));
        cbUF = new javax.swing.JComboBox();
        cbCidade = new javax.swing.JComboBox();
        jLabel17 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        pContatos = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        txtTelefoneResidencial = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        txtTelefoneComercial = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        txtTelefoneRecado = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        txtContato = new javax.swing.JFormattedTextField();
        txtCelular1 = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        txtCelular2 = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        txtCelular3 = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        txtCelular4 = new javax.swing.JFormattedTextField(getMask("## ####-####"));
        jLabel20 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel29 = new javax.swing.JLabel();
        pConvenio = new javax.swing.JPanel();
        cbConvenio = new javax.swing.JComboBox();
        cbPlano = new javax.swing.JComboBox();
        txtValidadeCarteirinha = new javax.swing.JFormattedTextField(getMask("##/##/####"));
        jScrollPane1 = new javax.swing.JScrollPane();
        txtObservacoes = new javax.swing.JEditorPane();
        jLabel32 = new javax.swing.JLabel();
        jLabel33 = new javax.swing.JLabel();
        jLabel35 = new javax.swing.JLabel();
        jLabel34 = new javax.swing.JLabel();
        btEditar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btSair = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Cadastro de Pacientes");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        pDadosPessoais.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados Pessoais"));

        txtCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCpfActionPerformed(evt);
            }
        });
        txtCpf.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCpfKeyPressed(evt);
            }
        });

        txtOrgaoEmissorRg.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtOrgaoEmissorRgKeyPressed(evt);
            }
        });

        txtDataEmissao.setPreferredSize(new java.awt.Dimension(62, 20));
        txtDataEmissao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDataEmissaoKeyPressed(evt);
            }
        });

        txtDataNascimento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDataNascimentoKeyPressed(evt);
            }
        });

        gpSexo.add(rbSexoFeminino);
        rbSexoFeminino.setText("Feminino");

        gpSexo.add(rbSexoMasculino);
        rbSexoMasculino.setSelected(true);
        rbSexoMasculino.setText("Masculino");

        cbCor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbProfissao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbEstadoCivil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Nome Completo*");

        jLabel7.setText("Data Emissão");

        jLabel8.setText("Orgão Emissor");

        jLabel5.setText("CPF*");

        jLabel6.setText("RG*");

        jLabel4.setText("Apelido");

        jLabel2.setText("Sexo");

        jLabel30.setText("Profissão");

        jLabel12.setText("Nome Mãe*");

        jLabel13.setText("Nome do Pai");

        jLabel9.setText("Data de Nas.*");

        jLabel3.setText("Cor");

        jLabel11.setText("Idade");

        jLabel10.setText("Estado Civil*");

        javax.swing.GroupLayout pDadosPessoaisLayout = new javax.swing.GroupLayout(pDadosPessoais);
        pDadosPessoais.setLayout(pDadosPessoaisLayout);
        pDadosPessoaisLayout.setHorizontalGroup(
            pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(326, 326, 326)
                        .addComponent(jLabel4))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 403, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(txtApelido))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addGap(121, 121, 121)
                        .addComponent(jLabel3)
                        .addGap(80, 80, 80)
                        .addComponent(jLabel30)
                        .addGap(88, 88, 88)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(txtIdade, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(rbSexoFeminino)
                        .addComponent(rbSexoMasculino)
                        .addGap(2, 2, 2)
                        .addComponent(cbCor, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(cbProfissao, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbEstadoCivil, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(210, 210, 210)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(txtNomePai, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNomeMae))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(153, 153, 153)
                                .addComponent(jLabel6)
                                .addGap(151, 151, 151)
                                .addComponent(jLabel8))
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(6, 6, 6)
                                .addComponent(txtRg, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(5, 5, 5)
                                .addComponent(txtOrgaoEmissorRg, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(jLabel7))
                            .addComponent(txtDataEmissao, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(txtDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        pDadosPessoaisLayout.setVerticalGroup(
            pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel8))
                                .addGap(6, 6, 6)
                                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtOrgaoEmissorRg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(6, 6, 6)
                                .addComponent(txtDataEmissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(6, 6, 6)
                        .addComponent(txtDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(6, 6, 6)
                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtApelido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel30)
                    .addComponent(jLabel10))
                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(txtIdade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(rbSexoFeminino)
                            .addComponent(rbSexoMasculino)
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(cbCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(cbProfissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(9, 9, 9)
                        .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel13)
                            .addComponent(jLabel12)))
                    .addGroup(pDadosPessoaisLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(cbEstadoCivil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(6, 6, 6)
                .addGroup(pDadosPessoaisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNomeMae, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNomePai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pEndereco.setBorder(javax.swing.BorderFactory.createTitledBorder("Endereço"));

        cbUF.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbCidade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel17.setText("UF*");

        jLabel16.setText("Bairro*");

        jLabel18.setText("CEP*");

        jLabel21.setText("Cidade*");

        jLabel19.setText("Complemento");

        jLabel14.setText("Endereço Residêncial*");

        jLabel15.setText("Número*");

        javax.swing.GroupLayout pEnderecoLayout = new javax.swing.GroupLayout(pEndereco);
        pEndereco.setLayout(pEnderecoLayout);
        pEnderecoLayout.setHorizontalGroup(
            pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pEnderecoLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pEnderecoLayout.createSequentialGroup()
                        .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jLabel15, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(jLabel19, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pEnderecoLayout.createSequentialGroup()
                        .addComponent(txtEndResidencial, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pEnderecoLayout.createSequentialGroup()
                        .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(142, 142, 142)
                        .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(80, 80, 80)
                        .addComponent(jLabel17)
                        .addGap(36, 36, 36)
                        .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pEnderecoLayout.createSequentialGroup()
                        .addComponent(txtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(txtCep, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addComponent(cbUF, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbCidade, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        pEnderecoLayout.setVerticalGroup(
            pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pEnderecoLayout.createSequentialGroup()
                .addGroup(pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel19))
                .addGap(1, 1, 1)
                .addGroup(pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtEndResidencial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel16)
                    .addComponent(jLabel18)
                    .addComponent(jLabel17)
                    .addComponent(jLabel21))
                .addGap(6, 6, 6)
                .addGroup(pEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pContatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Contatos"));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Telefones"));

        txtTelefoneResidencial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelefoneResidencialActionPerformed(evt);
            }
        });

        jLabel20.setText("Residêncial*");

        jLabel24.setText("Comercial");

        jLabel22.setText("Recado");

        jLabel23.setText("Contato");

        jLabel28.setText("Celular 4");

        jLabel27.setText("Celular 3");

        jLabel26.setText("Celular 2");

        jLabel25.setText("Celular 1*");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel20, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTelefoneResidencial, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel25, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCelular1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel24)
                    .addComponent(txtTelefoneComercial, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel26)
                    .addComponent(txtCelular2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTelefoneRecado, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel27)
                    .addComponent(txtCelular3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel23)
                    .addComponent(txtContato, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel28)
                    .addComponent(txtCelular4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel20)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel23)
                                .addComponent(jLabel22)))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTelefoneResidencial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelefoneRecado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel25)
                            .addComponent(jLabel27)
                            .addComponent(jLabel28))
                        .addGap(6, 6, 6)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCelular1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCelular3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCelular4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel24)
                        .addGap(6, 6, 6)
                        .addComponent(txtTelefoneComercial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel26)
                        .addGap(6, 6, 6)
                        .addComponent(txtCelular2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jLabel29.setText("e-mail");

        javax.swing.GroupLayout pContatosLayout = new javax.swing.GroupLayout(pContatos);
        pContatos.setLayout(pContatosLayout);
        pContatosLayout.setHorizontalGroup(
            pContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pContatosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pContatosLayout.createSequentialGroup()
                        .addComponent(jLabel29, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEmail))))
        );
        pContatosLayout.setVerticalGroup(
            pContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pContatosLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pContatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel29)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pConvenio.setBorder(javax.swing.BorderFactory.createTitledBorder("Convênio"));

        cbConvenio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbPlano.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        txtValidadeCarteirinha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtValidadeCarteirinhaActionPerformed(evt);
            }
        });
        txtValidadeCarteirinha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtValidadeCarteirinhaKeyPressed(evt);
            }
        });

        jScrollPane1.setViewportView(txtObservacoes);

        jLabel32.setText("Convênio*");

        jLabel33.setText("Plano*");

        jLabel35.setText("Observações:");

        jLabel34.setText("Validade*");

        javax.swing.GroupLayout pConvenioLayout = new javax.swing.GroupLayout(pConvenio);
        pConvenio.setLayout(pConvenioLayout);
        pConvenioLayout.setHorizontalGroup(
            pConvenioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pConvenioLayout.createSequentialGroup()
                .addComponent(jLabel32)
                .addGap(67, 67, 67)
                .addComponent(jLabel33)
                .addGap(103, 103, 103)
                .addComponent(jLabel34))
            .addGroup(pConvenioLayout.createSequentialGroup()
                .addComponent(cbConvenio, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(cbPlano, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(txtValidadeCarteirinha, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jLabel35)
            .addGroup(pConvenioLayout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        pConvenioLayout.setVerticalGroup(
            pConvenioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pConvenioLayout.createSequentialGroup()
                .addGroup(pConvenioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel32)
                    .addComponent(jLabel33)
                    .addComponent(jLabel34))
                .addGap(6, 6, 6)
                .addGroup(pConvenioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbConvenio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbPlano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtValidadeCarteirinha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addComponent(jLabel35)
                .addGap(2, 2, 2)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
        );

        btEditar.setText("Editar");
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });

        btSalvar.setText("Salvar");
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btSair.setText("Sair");
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pDadosPessoais, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pContatos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pConvenio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(788, Short.MAX_VALUE)
                .addComponent(btEditar)
                .addGap(9, 9, 9)
                .addComponent(btSalvar)
                .addGap(7, 7, 7)
                .addComponent(btSair)
                .addGap(54, 54, 54))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(pContatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pConvenio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(pDadosPessoais, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btEditar)
                    .addComponent(btSalvar)
                    .addComponent(btSair))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed

        //carregarDadosPaciente();//PARA TESTES


        if (validarPreenchimentoObrigatorio()) {
            IPacienteBO pacienteBO = new PacienteBO();
            carregarDadosPaciente();
            if (pacienteBO.salvarPaciente(this.paciente)) {
                JOptionPane.showMessageDialog(this, "Paciente salvo com sucesso");
            }
        }
}//GEN-LAST:event_btSalvarActionPerformed

    private void txtCpfKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCpfKeyPressed
        if (evt.getKeyCode() == 10) {
            if (txtCpf.getText().replace(" ", "").length() < 12) {
                JOptionPane.showMessageDialog(this, "Informe um cpf válido!");
            } else {
                IPacienteBO pacienteBO = new PacienteBO();
                Paciente pacienteVO = pacienteBO.consultarPaciente(txtCpf.getText());

                if (pacienteVO != null) {
                    this.paciente = pacienteVO;
                    exibirDadosPaciente();
                } else {
                    paciente = new Paciente();
                    liberarComponentes();
                }
            }
        }
}//GEN-LAST:event_txtCpfKeyPressed

    private Boolean validarPreenchimentoObrigatorio() {
        String mensagem1 = "Informe ";
        String mensagem2 = " do paciente!";
        if (txtCpf.getText().equals("")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o CPF" + mensagem2);
            txtCpf.setFocusable(true);
            return false;
        }

        if (txtRg.getText().replace(" ", "").equals("")) {
            txtRg.setFocusable(true);
            JOptionPane.showMessageDialog(this, mensagem1 + "o RG" + mensagem2);
            return false;
        }
        
        if (txtNome.getText().equals("")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Nome" + mensagem2);
            txtNome.setFocusable(true);
            return false;
        }
        //cbEstadoCivil.disable();
        if (txtDataNascimento.getText().replace(" ", "").equals("//")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "a Data de Dascimento" + mensagem2);
            txtDataNascimento.setFocusable(true);
            return false;
        }else {
            ValidarFormatarData(txtDataNascimento);
        }
        if (txtNomeMae.getText().equals("")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Nome da mãe" + mensagem2);
            txtNomeMae.setFocusable(true);
            return false;
        }
        if (txtEndResidencial.getText().equals("")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Endereço residencial" + mensagem2);
            txtEndResidencial.setFocusable(true);
            return false;
        }
        if (txtNumero.getText().equals("")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Número da residência" + mensagem2);
            txtNumero.setFocusable(true);
            return false;
        }
        if (txtBairro.getText().equals("")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Bairro" + mensagem2);
            txtBairro.setFocusable(true);
            return false;
        }
        if (txtCep.getText().replace(" ", "").equals("-")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o CEP" + mensagem2);
            txtCep.setFocusable(true);
            return false;
        }
        //cbUF.disable();
        //cbCidade.disable();
        if (txtTelefoneResidencial.getText().replace(" ", "").equals("-")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Telefone residencial" + mensagem2);
            txtTelefoneResidencial.setFocusable(true);
            return false;
        }
        if (txtCelular1.getText().replace(" ", "").equals("-")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "o Celular1" + mensagem2);
            txtCelular1.setFocusable(true);
            return false;
        }
        //cbConvenio.disable();
        //cbPlano.disable();
        if (txtValidadeCarteirinha.getText().replace(" ", "").equals("//")) {
            JOptionPane.showMessageDialog(this, mensagem1 + "a validade da carteirinha" + mensagem2);
            txtValidadeCarteirinha.setFocusable(true);
            return false;
        }else {
            ValidarFormatarData(txtValidadeCarteirinha
                    
                    );
        }
        
        return true;
    }

    /**
     * Esse metodo extrai o codigo do item seleciona no combo passado por parametro 
     * @param selectedItem
     * @return 
     */
    private Integer getCodigoItemSelecionadoCombo(JComboBox comboBox) {
        if (comboBox.getSelectedIndex() != 0) {
            String[] token = comboBox.getSelectedItem().toString().split("\\-");
            return Integer.parseInt(token[0]);
        } else {
            return null;
        }
    }

    /**
     * 
     */
    private void setarItemComboBox(JComboBox comboBox, Integer codigo) {
        comboBox.setSelectedIndex(codigo);
    }

    /**
     * Valida a data e
     * muda o formato de dd/MM/aaaa para yyyy-MM-dd
     * @param antigaData
     * @return 
     */
    private String ValidarFormatarData(JFormattedTextField componente) {
        //cria um array a partir da data informada
        String[] token = componente.getText().split("\\/");
        Integer dia = Integer.parseInt(token[0]);
        Integer mes = Integer.parseInt(token[0]);
        Integer ano = Integer.parseInt(token[0]);

        Boolean dataInvalida = false;
        //valida dia
        if ((Integer.parseInt(token[0]) < 1) || (Integer.parseInt(token[0]) > 31)) {
            dataInvalida = true;
        }
        //valida mês
        if ((Integer.parseInt(token[1]) < 1) || (Integer.parseInt(token[1]) > 12)) {
            dataInvalida = true;
        }
        //valida ano
        if ((Integer.parseInt(token[2]) < 1900) || (Integer.parseInt(token[2]) > 2020)) {
            dataInvalida = true;
        }

        if (dataInvalida) {
            JOptionPane.showMessageDialog(this, "Data inválida!");
            componente.setFocusable(true);
        }

        String novaData = "";

        //cria um string com o formato adequado ao banco
        for (int i = 2; i > -1; i--) {
            if (i == 0) {
                novaData += token[i];
            } else {
                novaData += token[i] + "-";
            }
        }

        return novaData;
    }

    /**
     * Metodo muda o formato de yyyy-MM-dd para dd/MM/aaaa 
     * @return 
     */
    private String FormatadorData(String antigaData) {
        String[] token = antigaData.split("\\-");
        String novaData = "";

        //cria um string com o formato dd/MM/aaaa
        for (int i = 2; i > -1; i--) {
            if (i == 0) {
                novaData += token[i];
            } else {
                novaData += token[i] + "/";
            }
        }

        return novaData;
    }

    private void carregarDadosPaciente() {
        //Dados Pessoais
        this.paciente.setCpf(txtCpf.getText());
        this.paciente.setNome(txtNome.getText());
        this.paciente.setApelido(txtApelido.getText());
        this.paciente.setRg(txtRg.getText());
        this.paciente.setOrgaoEmissor(txtOrgaoEmissorRg.getText());
        if (!txtDataEmissao.getText().replace(" ", "").equals("//")) {
            //this.paciente.setDataEmissao(Date.valueOf(txtDataEmissao.getText()));
            this.paciente.setDataEmissao(Date.valueOf(ValidarFormatarData(txtDataEmissao)));
        }

        this.paciente.setCodProfissao(getCodigoItemSelecionadoCombo(cbProfissao));
        this.paciente.setCodEstadoCivil(getCodigoItemSelecionadoCombo(cbEstadoCivil));

        if (!txtDataNascimento.getText().replace(" ", "").equals("//")) {
            this.paciente.setDataNascimento(Date.valueOf(ValidarFormatarData(txtDataNascimento)));
        }
        if (!txtIdade.getText().equals("")) {
            this.paciente.setIdade(Integer.parseInt(txtIdade.getText()));
        }

        this.paciente.setCodCor(getCodigoItemSelecionadoCombo(cbCor));

        if (rbSexoFeminino.isSelected()) {
            this.paciente.setSexo("F");
        } else if (rbSexoMasculino.isSelected()) {
            this.paciente.setSexo("M");
        }

        this.paciente.setNomeMae(txtNomeMae.getText());
        this.paciente.setNomePai(txtNomePai.getText());

        //Endereco
        this.paciente.setEndResidencial(txtEndResidencial.getText());
        this.paciente.setNumero(txtNumero.getText());
        this.paciente.setComplemento(txtComplemento.getText());
        this.paciente.setBairro(txtBairro.getText());
        this.paciente.setCep(txtCep.getText());

        this.paciente.setCodUf(getCodigoItemSelecionadoCombo(cbUF));
        this.paciente.setCodCidade(getCodigoItemSelecionadoCombo(cbCidade));

        //Contatos
        this.paciente.setTelResidencial(txtTelefoneResidencial.getText());
        this.paciente.setTelComercial(txtTelefoneComercial.getText());
        this.paciente.setTelRecado(txtTelefoneRecado.getText());
        this.paciente.setContato(txtContato.getText());
        this.paciente.setCel1(txtCelular1.getText());
        this.paciente.setCel2(txtCelular2.getText());
        this.paciente.setCel3(txtCelular3.getText());
        this.paciente.setCel4(txtCelular4.getText());
        this.paciente.setEmail(txtEmail.getText());

        this.paciente.setCodConvenio(getCodigoItemSelecionadoCombo(cbConvenio));
        this.paciente.setCodPlano(getCodigoItemSelecionadoCombo(cbPlano));

        if (!txtValidadeCarteirinha.getText().replace(" ", "").equals("//")) {
            this.paciente.setValidadeCarteira(Date.valueOf(ValidarFormatarData(txtValidadeCarteirinha)));
        }
        this.paciente.setObservacao(txtObservacoes.getText());
    }

    private void exibirDadosPaciente() {
        //Dados Pessoais
        txtNome.setText(paciente.getNome());
        txtApelido.setText(this.paciente.getApelido());
        txtRg.setText(this.paciente.getRg());
        txtOrgaoEmissorRg.setText(this.paciente.getOrgaoEmissor());
        txtDataEmissao.setText(FormatadorData(this.paciente.getDataEmissao().toString()));

        setarItemComboBox(cbProfissao, this.paciente.getCodProfissao());
        setarItemComboBox(cbEstadoCivil, this.paciente.getCodEstadoCivil());

        txtDataNascimento.setText(FormatadorData(this.paciente.getDataNascimento().toString()));
        txtIdade.setText(this.paciente.getIdade().toString());

        setarItemComboBox(cbCor, this.paciente.getCodCor());

        if (this.paciente.getSexo().equals("F")) {
            rbSexoFeminino.setSelected(true);
        } else if (this.paciente.getSexo().equals("M")) {
            rbSexoMasculino.setSelected(true);
        }

        txtNomeMae.setText(this.paciente.getNomeMae());
        txtNomePai.setText(this.paciente.getNomePai());

        //Endereco
        txtEndResidencial.setText(this.paciente.getEndResidencial());
        txtNumero.setText(this.paciente.getNumero());
        txtComplemento.setText(this.paciente.getComplemento());
        txtBairro.setText(this.paciente.getBairro());
        txtCep.setText(this.paciente.getCep());

        setarItemComboBox(cbUF, this.paciente.getCodUf());
        setarItemComboBox(cbCidade, this.paciente.getCodCidade());

        //Contatos
        txtTelefoneResidencial.setText(this.paciente.getTelResidencial());
        txtTelefoneComercial.setText(this.paciente.getTelComercial());
        txtTelefoneRecado.setText(this.paciente.getTelRecado());
        txtContato.setText(this.paciente.getContato());
        txtCelular1.setText(this.paciente.getCel1());
        txtCelular2.setText(this.paciente.getCel2());
        txtCelular3.setText(this.paciente.getCel3());
        txtCelular4.setText(this.paciente.getCel4());
        txtEmail.setText(this.paciente.getEmail());

        //Convenio
        setarItemComboBox(cbConvenio, this.paciente.getCodConvenio());
        setarItemComboBox(cbPlano, this.paciente.getCodPlano());
        txtValidadeCarteirinha.setText(FormatadorData(this.paciente.getValidadeCarteira().toString()));
        txtObservacoes.setText(this.paciente.getObservacao());

    }

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btSairActionPerformed

    public void carregarCombos() {
        String selecioneItem = "- Selecione -";
        IPacienteBO pbo = new PacienteBO();

        //profissao
        cbProfissao.removeAllItems();
        cbProfissao.addItem(selecioneItem);
        List<Profissao> listaProfissoes = pbo.listarProfissoes();
        for (Profissao profissao : listaProfissoes) {
            cbProfissao.addItem(profissao.getCodigo() + "-" + profissao.getDescricao());
        }

        //corPessoa
        cbCor.removeAllItems();
        cbCor.addItem(selecioneItem);
        List<CorPessoa> listaCorPessoa = pbo.listarCorPessoa();
        for (CorPessoa corPessoa : listaCorPessoa) {
            cbCor.addItem(corPessoa.getCodigo() + "-" + corPessoa.getDescricao());
        }

        //Cidade
        cbCidade.removeAllItems();
        cbCidade.addItem(selecioneItem);
        List<Cidade> listaCidade = pbo.listarCidades();
        for (Cidade cidade : listaCidade) {
            cbCidade.addItem(cidade.getCodigo() + "-" + cidade.getDescricao());
        }

        //UF
        cbUF.removeAllItems();
        cbUF.addItem(selecioneItem);
        List<Uf> listaUF = pbo.listarUF();
        for (Uf uf : listaUF) {
            cbUF.addItem(uf.getCodigo() + "-" + uf.getSigla());
        }

        //Plano
        cbPlano.removeAllItems();
        cbPlano.addItem(selecioneItem);
        List<Plano> listaPlano = pbo.listarPlano();
        for (Plano plano : listaPlano) {
            cbPlano.addItem(plano.getCodigo() + "-" + plano.getDescricao());
        }

        //Convenio
        cbConvenio.removeAllItems();
        cbConvenio.addItem(selecioneItem);
        List<Convenio> listaConvenio = pbo.listarConvenio();
        for (Convenio convenio : listaConvenio) {
            cbConvenio.addItem(convenio.getCodigo() + "-" + convenio.getDescricao());
        }

        //EstadoCivil
        cbEstadoCivil.removeAllItems();
        cbEstadoCivil.addItem(selecioneItem);
        List<EstadoCivil> listaEstadoCivil = pbo.listarEstadoCivil();
        for (EstadoCivil estadoCivil : listaEstadoCivil) {
            cbEstadoCivil.addItem(estadoCivil.getCodigo() + "-" + estadoCivil.getDescricao());
        }
    }

    public void bloquearComponentes() {
        //Dados Pessoais
        txtNome.disable();
        txtApelido.disable();
        txtRg.disable();
        txtOrgaoEmissorRg.disable();
        txtDataEmissao.disable();
        cbProfissao.disable();
        cbEstadoCivil.disable();
        txtDataNascimento.disable();
        txtIdade.disable();
        cbCor.disable();
        rbSexoFeminino.disable();
        rbSexoMasculino.disable();
        txtNomeMae.disable();
        txtNomePai.disable();

        //Endereco
        txtEndResidencial.disable();
        txtNumero.disable();
        txtComplemento.disable();
        txtBairro.disable();
        txtCep.disable();
        cbUF.disable();
        cbCidade.disable();

        //Contatos
        txtTelefoneResidencial.disable();
        txtTelefoneComercial.disable();
        txtTelefoneRecado.disable();
        txtContato.disable();
        txtCelular1.disable();
        txtCelular2.disable();
        txtCelular3.disable();
        txtCelular4.disable();
        txtEmail.disable();

        //Convenio
        cbConvenio.disable();
        cbPlano.disable();
        txtValidadeCarteirinha.disable();
        txtObservacoes.disable();

        this.repaint();
    }

    private void liberarComponentes() {
        //Dados Pessoais
        txtNome.enable();
        txtApelido.enable();
        txtRg.enable();
        txtOrgaoEmissorRg.enable();
        txtDataEmissao.enable();
        cbProfissao.enable();
        cbEstadoCivil.enable();
        txtDataNascimento.enable();
        txtIdade.enable();
        cbCor.enable();
        rbSexoFeminino.enable();
        rbSexoMasculino.enable();
        txtNomeMae.enable();
        txtNomePai.enable();

        //Endereco
        txtEndResidencial.enable();
        txtNumero.enable();
        txtComplemento.enable();
        txtBairro.enable();
        txtCep.enable();
        cbUF.enable();
        cbCidade.enable();

        //Contatos
        txtTelefoneResidencial.enable();
        txtTelefoneComercial.enable();
        txtTelefoneRecado.enable();
        txtContato.enable();
        txtCelular1.enable();
        txtCelular2.enable();
        txtCelular3.enable();
        txtCelular4.enable();
        txtEmail.enable();

        //Convenio
        cbConvenio.enable();
        cbPlano.enable();
        txtValidadeCarteirinha.enable();
        txtObservacoes.enable();

        this.repaint();
    }

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
    }//GEN-LAST:event_formInternalFrameOpened

    private void txtCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCpfActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        if (this.paciente != null) {
            liberarComponentes();
        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void txtTelefoneResidencialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelefoneResidencialActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelefoneResidencialActionPerformed

    private void txtValidadeCarteirinhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtValidadeCarteirinhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtValidadeCarteirinhaActionPerformed

    private void txtOrgaoEmissorRgKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtOrgaoEmissorRgKeyPressed
    }//GEN-LAST:event_txtOrgaoEmissorRgKeyPressed

    private void txtDataEmissaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDataEmissaoKeyPressed
        validarCampos(evt, txtDataEmissao);
    }//GEN-LAST:event_txtDataEmissaoKeyPressed

    private void validarCampos(java.awt.event.KeyEvent evt, JComponent componente) {
        if (evt.getKeyCode() == 10) {
            if (componente instanceof JFormattedTextField) {
                JFormattedTextField comp = (JFormattedTextField) componente;
                
                if (((JFormattedTextField) componente).getFormatter().equals("#########-##")) {
                    if (txtCpf.getText().replace(" ", "").length() < 12) {
                        JOptionPane.showMessageDialog(this, "Informe um cpf válido!");
                        componente.setFocusable(true);
                    }
                } else {
                    //Campos de data
                    if (ValidarFormatarData(((JFormattedTextField) componente)).equals("")) {
                        ((JFormattedTextField) componente).setFocusable(true);
                        componente.setFocusable(true);
                    }
                }
            }

            if (componente instanceof JTextField) {
                if (((JTextField) componente).getName().equals("txtEmail")) {
                    JOptionPane.showMessageDialog(this, "O campo email deve ter no máximo 50 caracteres.");
                    componente.setFocusable(true);
                }
            }
        }
    }

    private void txtDataNascimentoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDataNascimentoKeyPressed
        validarCampos(evt, txtDataNascimento);
    }//GEN-LAST:event_txtDataNascimentoKeyPressed

    private void txtValidadeCarteirinhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValidadeCarteirinhaKeyPressed
        validarCampos(evt, txtValidadeCarteirinha);
    }//GEN-LAST:event_txtValidadeCarteirinhaKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEditar;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbCidade;
    private javax.swing.JComboBox cbConvenio;
    private javax.swing.JComboBox cbCor;
    private javax.swing.JComboBox cbEstadoCivil;
    private javax.swing.JComboBox cbPlano;
    private javax.swing.JComboBox cbProfissao;
    private javax.swing.JComboBox cbUF;
    private javax.swing.ButtonGroup gpSexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pContatos;
    private javax.swing.JPanel pConvenio;
    private javax.swing.JPanel pDadosPessoais;
    private javax.swing.JPanel pEndereco;
    private javax.swing.JRadioButton rbSexoFeminino;
    private javax.swing.JRadioButton rbSexoMasculino;
    private javax.swing.JTextField txtApelido;
    private javax.swing.JTextField txtBairro;
    private javax.swing.JFormattedTextField txtCelular1;
    private javax.swing.JFormattedTextField txtCelular2;
    private javax.swing.JFormattedTextField txtCelular3;
    private javax.swing.JFormattedTextField txtCelular4;
    private javax.swing.JFormattedTextField txtCep;
    private javax.swing.JTextField txtComplemento;
    private javax.swing.JFormattedTextField txtContato;
    private javax.swing.JFormattedTextField txtCpf;
    private javax.swing.JFormattedTextField txtDataEmissao;
    private javax.swing.JFormattedTextField txtDataNascimento;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtEndResidencial;
    private javax.swing.JTextField txtIdade;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtNomeMae;
    private javax.swing.JTextField txtNomePai;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JEditorPane txtObservacoes;
    private javax.swing.JFormattedTextField txtOrgaoEmissorRg;
    private javax.swing.JFormattedTextField txtRg;
    private javax.swing.JFormattedTextField txtTelefoneComercial;
    private javax.swing.JFormattedTextField txtTelefoneRecado;
    private javax.swing.JFormattedTextField txtTelefoneResidencial;
    private javax.swing.JFormattedTextField txtValidadeCarteirinha;
    // End of variables declaration//GEN-END:variables
}
